package Windows;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import org.json.JSONObject;

import Bean.Msg;
import MainThread.MyClient;

import javax.swing.JScrollPane;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JTextArea;
import javax.swing.JButton;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.awt.event.ActionEvent;
import javax.swing.DropMode;

public class iTalk extends JFrame {

	private Socket s;
	private JPanel contentPane;
	private JPanel panel_1;
	private JTextArea T_SendMsg;
	private JButton B_MegSend;
	private JPanel panel_2;
	private JPanel panel_3;
	private JLabel ChatFrom;
	private JTextArea T_talkContent;

	private String toFriend;
	private String mySelf;
	/**
	 * Launch the application.
	 */
	// public static void main(String[] args) {
	// EventQueue.invokeLater(new Runnable() {
	// public void run() {
	// try {
	// iTalk frame = new iTalk();
	// frame.setVisible(true);
	// } catch (Exception e) {
	// e.printStackTrace();
	// }
	// }
	// });
	// }

	/**
	 * Create the frame.
	 */
	public iTalk(Socket s, String toFriend, String mySelf) {
		this.s = s;
		this.toFriend = toFriend;
		// setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 975, 608);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);

		panel_1 = new JPanel();
		contentPane.add(panel_1, BorderLayout.SOUTH);

		// Set<String> Juser = new LinkedHashSet<>();

		B_MegSend = new JButton("发送");
		B_MegSend.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					PrintStream ps = new PrintStream(s.getOutputStream());
					String line = T_SendMsg.getText();
					if (line != null) {
						if(FriendTree.friendLine.contains(toFriend)){
							setT_talkContent("我:"+line+"\n");
							T_SendMsg.setText("");
							Msg msg = new Msg();
							msg.setLogin_reg_chat_ack(MyClient.CHAT);
							msg.setContent(line);
							msg.setFrom(mySelf);
							msg.setTo(toFriend);
							ps.println(new JSONObject(msg));
							msg = null;
						}else{
							T_talkContent.append("用户已离线，无法发送");
						}
					}
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
		});

		JScrollPane scrollPane = new JScrollPane();
		GroupLayout gl_panel_1 = new GroupLayout(panel_1);
		gl_panel_1.setHorizontalGroup(gl_panel_1.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_panel_1.createSequentialGroup().addContainerGap()
						.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 857, Short.MAX_VALUE)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(B_MegSend, GroupLayout.DEFAULT_SIZE, 66, Short.MAX_VALUE).addContainerGap()));
		gl_panel_1.setVerticalGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_1.createSequentialGroup().addContainerGap()
						.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
								.addComponent(B_MegSend, GroupLayout.DEFAULT_SIZE, 157, Short.MAX_VALUE)
								.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 157, Short.MAX_VALUE))));

		T_SendMsg = new JTextArea();
		scrollPane.setViewportView(T_SendMsg);
		panel_1.setLayout(gl_panel_1);

		panel_2 = new JPanel();
		contentPane.add(panel_2, BorderLayout.CENTER);
		panel_2.setLayout(new BorderLayout(0, 0));

		panel_3 = new JPanel();
		panel_2.add(panel_3, BorderLayout.NORTH);

		ChatFrom = new JLabel("和" + toFriend + "聊天中...");
		ChatFrom.setFont(new Font("黑体", Font.PLAIN, 16));
		panel_3.add(ChatFrom);

		JScrollPane scrollPane_1 = new JScrollPane();
		panel_2.add(scrollPane_1, BorderLayout.CENTER);

		T_talkContent = new JTextArea();
		// T_taklContent.setDropMode(DropMode.ON);
		T_talkContent.setFont(new Font("微软雅黑", Font.PLAIN, 13));
		T_talkContent.setEditable(false);
		T_talkContent.setWrapStyleWord(true);
		scrollPane_1.setViewportView(T_talkContent);
		// 将自己添加进friendiTalk的Map集合中
		FriendTree.friendiTalk.put(mySelf, this);

		// 窗口监听关闭事件
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent we) {
				FriendTree.friendiTalk.remove(toFriend);
			}
		});
	}

	public JTextArea getT_SendMsg() {
		return T_SendMsg;
	}

	public void setT_SendMsg(JTextArea t_SendMsg) {
		T_SendMsg = t_SendMsg;
	}

	public JButton getB_MegSend() {
		return B_MegSend;
	}

	public void setB_MegSend(JButton b_MegSend) {
		B_MegSend = b_MegSend;
	}

	public JTextArea getT_talkContent() {
		return T_talkContent;
	}

	public void setT_talkContent(String content) {
		T_talkContent.append(content);
	}
}
